#include <learn/painter/painter3d.h>
#include <learn/painter/win_ctx.h>

using namespace xi::painter;
using namespace std;

void init_proc(const Paint_Context *ctx);
void deal_proc(const Paint_Context *ctx);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    Win_Context window(hInstance, nCmdShow, 100, 100, 800, 800, init_proc, deal_proc, L"My Demo");
    return window.message_loop();
}

// 当前路径
#define SOURCE_PATH ROOT_SOURCE_PATH "/src/my/my2d/"

namespace
{

TGA_Color white(255, 255, 255);
TGA_Color black(0, 0, 0);
TGA_Color red(255, 0, 0);
TGA_Color green(0, 255, 0);
TGA_Color blue(0, 0, 255);
TGA_Color yellow(255, 255, 0);
TGA_Color pink(192, 203, 0);

} // namespace

void init_proc(const Paint_Context *ctx)
{
}

void deal_proc(const Paint_Context *ctx)
{
    auto frame = ctx->painter()->frame();
    auto painter = std::make_shared<Painter2D>(frame->width, frame->height);
    auto bitmap = painter->frame();

    painter->refresh();
    painter->draw_line(0, 0, bitmap->width * (1 + sin(painter->duration() / 1000.0)) / 2, 120, red);
    painter->draw_line(50, 50, 50, 450, blue);
    painter->draw_triangle(100, 100, 200, 200, 300, 450, yellow);
    painter->fill_triangle(100, 100, 200, 200, 300, 450, red);
    painter->draw_rectangle(100, 100, 200, 200, red);
    painter->fill_rectangle(100, 100, 200, 200, yellow);
    // painter->draw_image(0, 0, L"medianBlur.jpg", L"jpg");
    // painter->draw_image(0, 0, L"output-small.png", L"png");

    // DrawLine(200, 200, 200 + 100 * cos(duration / 1000.0), 200 + 100 * sin(duration / 1000.0), red);
    // DrawLineAntialias(400, 200, 400 + 100 * cos(duration / 1000.0), 200 + 100 * sin(duration / 1000.0), yellow, 5);

    constexpr float PI = 3.1415926;
    float r = 200;
    int M = 5, N = 30;
    int cx = 300, cy = 220;
    for (int i = 1; i < M; i++)
    {
        float phase = 0.1 * i;
        float r1 = i * r / M;
        float r2 = (i + 1) * 40;
        for (int j = 0; j < N; j++)
        {
            float theta = 2 * PI / N * j + phase;
            // painter->draw_line(cx + r1 * cos(theta), cy + r1 * sin(theta), cx + r2 * cos(theta), cy + r2 *
            // sin(theta),
            //          yellow);
            painter->draw_line_antialias(cx + r1 * cos(theta), cy + r1 * sin(theta), cx + r2 * cos(theta),
                                         cy + r2 * sin(theta), yellow, i);
        }
    }

    painter->draw_circle(400, 200, 50, yellow);
    painter->draw_circle_pointwise(500, 200, 50, yellow);
    painter->draw_circle_bresenham(600, 200, 50, yellow);

    // painter->draw_tga_image(0, 0, TEST_DATA_PATH "/floor_diffuse.tga");
    // painter->draw_ortho_object(-800 * 2, 0, TEST_DATA_PATH "/head.obj", 5);
    // painter->draw_ortho_object(-800 * 2, 0, TEST_DATA_PATH "/african_head.obj", 5);
    // painter->fill_boundary_4(500, 200, red, yellow);
    // painter->fill_boundary_4(549, 200, red, yellow);
    // painter->fill_flood_4(600, 200, blue, black);

    // auto pos = random_point<2>(100, 200, time(0));
    // DrawPoint(pos.X(), pos.Y(), green, 10);

    // static std::vector<BPnt2i> points;
    // if (points.empty())
    //{
    //     for (int i = 0; i < 5; i++)
    //         points.push_back(random_point<2>(100, 400, time(0)));
    // }

    //// DrawPolyline(points, blue);
    // DrawPolygon(points, blue);

    // DrawEllispe(100, 100, 200, 300, red);
    // DrawRectangle(100, 100, 200, 300, yellow);

    // auto f = [](double x) -> double { return 100 + 50 * sin(x / 50); };
    //  DrawFunction(f, 100, 100 + duration / 10 % 200, 100, green, 2);
    // DrawFunction(f, 100, 300, 100, green, 2);

    // painter->DrawObject(0, 0, "head.obj", 5.0);

    // 将 2D 画布的图像数据更新到 3D 画布中
    ctx->painter()->set_frame(painter->frame());
}
